package com.castle.fortress.admin.member.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.study.exam.entity.StudyQuestionEntity;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.member.entity.MemberGroupEntity;
import com.castle.fortress.admin.member.dto.MemberGroupDto;
import com.castle.fortress.admin.member.mapper.MemberGroupMapper;
import com.castle.fortress.admin.member.service.MemberGroupService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 会员分组表 服务实现类
 *
 * @author castle
 * @since 2023-02-13
 */
@Service
public class MemberGroupServiceImpl extends ServiceImpl<MemberGroupMapper, MemberGroupEntity> implements MemberGroupService {
    /**
    * 获取查询条件
    * @param memberGroupDto
    * @return
    */
    public QueryWrapper<MemberGroupEntity> getWrapper(MemberGroupDto memberGroupDto){
        QueryWrapper<MemberGroupEntity> wrapper= new QueryWrapper();
        if(memberGroupDto != null){
            MemberGroupEntity memberGroupEntity = ConvertUtil.transformObj(memberGroupDto,MemberGroupEntity.class);
            wrapper.like(StrUtil.isNotEmpty(memberGroupEntity.getName()),"name",memberGroupEntity.getName());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<MemberGroupDto> pageMemberGroup(Page<MemberGroupDto> page, MemberGroupDto memberGroupDto) {
        QueryWrapper<MemberGroupEntity> wrapper = getWrapper(memberGroupDto);
        Page<MemberGroupEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<MemberGroupEntity> memberGroupPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<MemberGroupDto> pageDto = new Page(memberGroupPage.getCurrent(), memberGroupPage.getSize(),memberGroupPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(memberGroupPage.getRecords(),MemberGroupDto.class));
        return pageDto;
    }

    @Override
    public IPage<MemberGroupDto> pageMemberGroupExtends(Page<MemberGroupDto> page, MemberGroupDto memberGroupDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        MemberGroupEntity memberGroupEntity = ConvertUtil.transformObj(memberGroupDto,MemberGroupEntity.class);
        List<MemberGroupEntity> memberGroupList=baseMapper.extendsList(pageMap,memberGroupEntity);
        Long total = baseMapper.extendsCount(memberGroupEntity);
        Page<MemberGroupDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(memberGroupList,MemberGroupDto.class));
        return pageDto;
    }
    @Override
    public MemberGroupDto getByIdExtends(Long id){
        MemberGroupEntity  memberGroupEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(memberGroupEntity,MemberGroupDto.class);
    }

    @Override
    public List<MemberGroupDto> listMemberGroup(MemberGroupDto memberGroupDto){
        QueryWrapper<MemberGroupEntity> wrapper = getWrapper(memberGroupDto);
        List<MemberGroupEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,MemberGroupDto.class);
    }

    @Override
    public boolean checkName(MemberGroupEntity memberGroupEntity) {
        QueryWrapper<MemberGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("name",memberGroupEntity.getName());
        wrapper.ne(memberGroupEntity.getId()!=null,"id",memberGroupEntity.getId());
        List<MemberGroupEntity> list = baseMapper.selectList(wrapper);
        return (list!=null&&list.size()>0)?true:false;
    }
}

